package com.qfedu.mesadmin.md.unit.controller;

import com.qfedu.mes.common.CommonResult;
import com.qfedu.mes.common.PageVO;
import com.qfedu.mes.constrants.CommonConstants;
import com.qfedu.mes.entity.md.MdUnitMeasure;
import com.qfedu.mes.entity.sys.SysUser;
import com.qfedu.mesadmin.md.unit.params.AddUnitParam;
import com.qfedu.mesadmin.md.unit.params.UnitPageParam;
import com.qfedu.mesadmin.md.unit.service.MdUnitMeasureService;
import com.qfedu.mesadmin.md.unit.vo.PrimaryUnitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/md/unit")
public class MdUnitMeasureController {

    @Resource
    private MdUnitMeasureService mdUnitMeasureService;

    @GetMapping("/list")
    public CommonResult listUnits() {
        List<MdUnitMeasure> mdUnitMeasures = mdUnitMeasureService.listUnits();
        return CommonResult.success(mdUnitMeasures);
    }

    @GetMapping("/page")
    public CommonResult pageUnits(UnitPageParam param) {
        PageVO<MdUnitMeasure> pageVO = mdUnitMeasureService.pageUnit(param);
        return CommonResult.success(pageVO);
    }

    @PostMapping("/add")
    public CommonResult addUnit(@RequestBody AddUnitParam addUnitParam, HttpSession session) {

        SysUser sysUser = (SysUser) session.getAttribute(CommonConstants.LOGIN_INFO);
        String userName = sysUser.getUserName();

        MdUnitMeasure mdUnitMeasure = new MdUnitMeasure();
        // 笨方法
        // mdUnitMeasure.setMeasureCode(addUnitParam.getMeasureCode());
        // 对象属性的拷贝，两个对象属性名需要相同
        BeanUtils.copyProperties(addUnitParam, mdUnitMeasure);
        mdUnitMeasure.setCreateBy(userName);

        mdUnitMeasureService.addUnit(mdUnitMeasure);
        return CommonResult.success();
    }

    @GetMapping("/list/primary")
    public CommonResult listPrimaryUnit() {
        List<PrimaryUnitVO> primaryUnitVOS = mdUnitMeasureService.listPrimaryUnit();
        return CommonResult.success(primaryUnitVOS);
    }

    @GetMapping("/list/all")
    public CommonResult listAllUnit() {
        List<PrimaryUnitVO> primaryUnitVOS = mdUnitMeasureService.listAllUnit();
        return CommonResult.success(primaryUnitVOS);
    }


    @GetMapping("/delete")
    public CommonResult deleteUnit(Integer id) {
        mdUnitMeasureService.deleteUnit(id);
        return CommonResult.success();
    }

    @PostMapping("/delete/batch")
    public CommonResult deleteUnitBatch(@RequestBody List<Integer> ids) {
        mdUnitMeasureService.deleteUnitBatch(ids);
        return CommonResult.success();
    }

    @GetMapping("/info")
    public CommonResult unitInfo(Integer id) {
        MdUnitMeasure mdUnitMeasure = mdUnitMeasureService.unitInfo(id);
        return CommonResult.success(mdUnitMeasure);
    }

    @PostMapping("/update")
    public CommonResult updateUnit(@RequestBody MdUnitMeasure mdUnitMeasure) {
        mdUnitMeasureService.updateUnit(mdUnitMeasure);
        return CommonResult.success();
    }
}
